﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
    /// <summary>
    /// 提供一致的资源管理API
    /// </summary>
    public sealed class AcorisMarkdownResourceManager
    {

        //---------------------------------------------------------------------------------------------------
        //  Interaction -> Require Resource (Action)-> Resource Manager 
        //  -> RequireResourceEventArgs -> View Handler
        //  
        //---------------------------------------------------------------------------------------------------
        Dictionary<string,AcorisMarkdownResource> _dictionary;

        #region ResourceGenerator Defination


        //---------------------------------------------------------------------------------------------------
        //  Local Image Resource Gen
        //  Need:
        //  1) Local Image Path
        //  2) Resource Key
        //
        //  Local Reference
        //  Need:
        //  1) Local Page Reference
        //  2) Resource Key
        //
        //  Outside Reference
        //  1) Outside Path
        //  2) Resource Key
        //---------------------------------------------------------------------------------------------------
        interface IResourceGenerator
        {
            /// <summary>
            /// 从资源包中加载指定的资源
            /// </summary>
            /// <param name="resource">指定需要加载的资源</param>
            void LoadFromResourcePackage(AcorisMarkdownResource resource);

            /// <summary>
            /// 加载外部资源
            /// </summary>
            /// <param name="resource"></param>
            void LoadFromOutside(AcorisMarkdownResource resource);

            /// <summary>
            /// 将指定的资源写入资源包
            /// </summary>
            /// <param name="resource">指定需要保存的资源</param>
            /// <param name="param">指定需要保存的资源额外参数</param>
            void SaveToResourcePackage(AcorisMarkdownResource resource, object param);

            /// <summary>
            /// 将指定资源从资源包中移除
            /// </summary>
            /// <param name="resource"></param>
            void RemoveResource(AcorisMarkdownResource resource);
        }

        sealed class LocalAudioResourceGenerator : IResourceGenerator
        {
            public void LoadFromOutside(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void LoadFromResourcePackage(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void RemoveResource(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void SaveToResourcePackage(AcorisMarkdownResource resource, object param)
            {
                throw new NotImplementedException();
            }
        }

        sealed class LocalImageResourceGenerator : IResourceGenerator
        {
            public void LoadFromOutside(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void LoadFromResourcePackage(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void RemoveResource(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void SaveToResourcePackage(AcorisMarkdownResource resource, object param)
            {
                throw new NotImplementedException();
            }
        }

        sealed class LocalInteractionResourceGenerator : IResourceGenerator
        {
            public void LoadFromOutside(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void LoadFromResourcePackage(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void RemoveResource(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void SaveToResourcePackage(AcorisMarkdownResource resource, object param)
            {
                throw new NotImplementedException();
            }
        }

        sealed class LocalVideoResourceGenerator : IResourceGenerator
        {
            public void LoadFromOutside(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void LoadFromResourcePackage(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void RemoveResource(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void SaveToResourcePackage(AcorisMarkdownResource resource, object param)
            {
                throw new NotImplementedException();
            }
        }

        sealed class OutsideReferenceGenerator : IResourceGenerator
        {
            public void LoadFromOutside(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void LoadFromResourcePackage(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void RemoveResource(AcorisMarkdownResource resource)
            {
                throw new NotImplementedException();
            }

            public void SaveToResourcePackage(AcorisMarkdownResource resource, object param)
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region Fields

        IResourceGenerator _videoGen;
        IResourceGenerator _audioGen;
        IResourceGenerator _imageGen;
        IResourceGenerator _interactionGen;
        IResourceGenerator _outsideRefGen;

        #endregion

        #region Constructor


        public AcorisMarkdownResourceManager()
        {
            _dictionary = new Dictionary<string, AcorisMarkdownResource>();
            _videoGen = new LocalVideoResourceGenerator();
            _audioGen = new LocalAudioResourceGenerator();
            _imageGen = new LocalImageResourceGenerator();
            _interactionGen = new LocalInteractionResourceGenerator();
            _outsideRefGen = new OutsideReferenceGenerator();
        }

        #endregion

        #region Method

        private IResourceGenerator GetResourceGenerator(AcorisMarkdownResource resource)
        {
            switch (resource.Type) {
                case ResourceType.Audio:
                    return _audioGen;
                case ResourceType.Image:
                    return _imageGen;
                case ResourceType.Interaction:
                    return _interactionGen;
                case ResourceType.OutsideImage:
                case ResourceType.OutsideReference:
                case ResourceType.OutsideVideo:
                    return _outsideRefGen;
                case ResourceType.Video:
                    return _videoGen;
            }
            return null;
        }

        #endregion

        #region Property

        public AcorisMarkdownResource this[string key] {
            get {
                if(_dictionary.TryGetValue(key,out var val)) {
                    return val;
                }
                return null;
            }
            set {
                if(_dictionary.TryGetValue(key,out var val)) {
                    _dictionary[key] = value;
                }
                throw new KeyNotFoundException();
            }
        }

        #endregion
    }
}
